/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import java.util.ArrayList;

/**
 * Skupina checkboxu. Virtualni vec, ktera zarizuje, aby z nekolika policek
 * mohlo najednou byt zaskrtnuto jen jedno
 *
 * @author Ondra
 */
public class LCheckboxGroup implements CheckboxListener {

    private ArrayList<LCheckbox> checkboxes;
    private boolean allFalseAllowed;

    /**
     *
     * @param allFalseAllowed true - mohou byt vsechna tlacitka falae. false -
     * vzdy bude alespon jedno true
     */
    public LCheckboxGroup(boolean allFalseAllowed) {
        this.checkboxes = new ArrayList();
        this.allFalseAllowed = allFalseAllowed;
    }

    /**
     * prida policko do skupiny
     *
     * @param checkbox
     */
    public void add(LCheckbox checkbox) {
        checkboxes.add(checkbox);
        checkbox.addCheckboxListener(this);
        controlStates(checkbox);
    }

    private void controlStates(LCheckbox source) {
        if (source.getState()) {
            for (LCheckbox c : checkboxes) {
                if (!(c == source)) {
                    c.setState(false);
                }
            }
        } else if (!allFalseAllowed) {
            for (LCheckbox c : checkboxes) {
                if (c.getState()) {
                    return;
                }
            }
            if(checkboxes.size() > 0){
                checkboxes.get(0).setState(true);
            }
        }
    }

    @Override
    public void checkboxStateChanged(CheckboxEvent e) {
        controlStates((LCheckbox) e.getSource());
    }
}
